package 剑指offer;


/*
 * Author：江松
 * Date：2023/2/27 21:10
 *
 *
 * 快速幂：3^8=(3*3)^(8/2)=(9*9)^(4/2)=81^2
 */

public class Main8 {
    public long qpow(long base,int pow){
        long ans=1;
        while(pow>0){
            if((pow&1)==1){
                ans*=base;
            }
            base*=base;
            pow>>=1;
        }
        return ans;
    }

    public double Power(double base, int exponent) {
        int pow=Math.abs(exponent);

        double ans=1;
        while(pow>0){
            if((pow&1)==1)ans*=base;
            base*=base;
            pow>>=1;
        }
        return exponent>=0?ans:1/ans;
    }

    public static void main(String[] args) {
        Main8 m8=new Main8();
        System.out.println(m8.qpow(3,5));
        System.out.println(m8.Power(3,-5));
    }
}
